<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="erp_balance的主键" prop="balanceId">
        <el-input v-model="formData.balanceId" placeholder="请输入erp_balance的主键" />
      </el-form-item>
      <el-form-item label="报表日期" prop="reportDate">
        <el-date-picker
          v-model="formData.reportDate"
          type="date"
          value-format="x"
          placeholder="选择报表日期"
        />
      </el-form-item>
      <el-form-item label="合约类型" prop="nameCategory">
        <el-input v-model="formData.nameCategory" placeholder="请输入合约类型" />
      </el-form-item>
      <el-form-item label="合约名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入合约名称" />
      </el-form-item>
      <el-form-item label="交易类型" prop="buySell">
        <el-radio-group v-model="formData.buySell">
          <el-radio
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_BUY_SELL_TYPE)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手续费" prop="chargeFee">
        <el-input v-model="formData.chargeFee" placeholder="请输入手续费" />
      </el-form-item>
      <el-form-item label="盈亏金额" prop="amount">
        <el-input v-model="formData.amount" placeholder="请输入盈亏金额" />
      </el-form-item>
      <el-form-item label="手数" prop="lots">
        <el-input v-model="formData.lots" placeholder="请输入手数" />
      </el-form-item>
      <el-form-item label="交易保证金" prop="tradingMargin">
        <el-input v-model="formData.tradingMargin" placeholder="请输入交易保证金" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { ProfitLossDayApi, ProfitLossDayVO } from '@/api/erp/profitlossday'

/** 每日盈亏 表单 */
defineOptions({ name: 'ProfitLossDayForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  balanceId: undefined,
  reportDate: undefined,
  nameCategory: undefined,
  name: undefined,
  buySell: undefined,
  chargeFee: undefined,
  amount: undefined,
  lots: undefined,
  tradingMargin: undefined,
})
const formRules = reactive({
  reportDate: [{ required: true, message: '报表日期不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '合约名称不能为空', trigger: 'blur' }],
  chargeFee: [{ required: true, message: '手续费不能为空', trigger: 'blur' }],
  amount: [{ required: true, message: '盈亏金额不能为空', trigger: 'blur' }],
  lots: [{ required: true, message: '手数不能为空', trigger: 'blur' }],
  tradingMargin: [{ required: true, message: '交易保证金不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProfitLossDayApi.getProfitLossDay(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProfitLossDayVO
    if (formType.value === 'create') {
      await ProfitLossDayApi.createProfitLossDay(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProfitLossDayApi.updateProfitLossDay(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    balanceId: undefined,
    reportDate: undefined,
    nameCategory: undefined,
    name: undefined,
    buySell: undefined,
    chargeFee: undefined,
    amount: undefined,
    lots: undefined,
    tradingMargin: undefined,
  }
  formRef.value?.resetFields()
}
</script>